package com.rookieci.totp.db;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.rookieci.totp.BuildConfig;
import com.rookieci.totp.TotpApp;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by 磁磁帅 on 2025/8/10
 * 功能：计划做个数据库连接池，但是可能用不上
 */
public class DbConnect {
    private static final String TAG = DbConnect.class.getSimpleName();
    private static DbConnect dbConnect;
    private ReentrantReadWriteLock.WriteLock writeLock;//获取到表示可以使用
    private DBHelp dbHelp;
    private SQLiteDatabase[] database;//0读 1写
    private int closeTime;
    private Runnable closeRunnable;

    public static DbConnect getInstance() {
        if (dbConnect == null) {
            synchronized (DbConnect.class) {
                if (dbConnect == null) dbConnect = new DbConnect();
            }
        }
        return dbConnect;
    }

    private DbConnect() {
        database = new SQLiteDatabase[2];
    }

    public DbConnect connect() {
        getDbHelp();
        if (BuildConfig.DEBUG) Log.i(TAG, "--数据库-连接");
        return this;
    }

    public DBHelp getDbHelp() {
        synchronized (DbConnect.class) {
            writeLock = MyDBLock.getInstance().writeLock();
            if (dbHelp == null) dbHelp = new DBHelp(TotpApp.getInstance());
        }
        return dbHelp;
    }

    public DbConnect openReadable() {
        if (database[1] != null) {
            database[1].close();
            database[1] = null;
        }
        if (database[0] == null) database[0] = dbHelp.getReadableDatabase();
        return this;
    }

    public DbConnect openWritable() {
        if (database[0] != null) {
            database[0].close();
            database[0] = null;
        }
        if (database[1] == null) database[1] = dbHelp.getWritableDatabase();
        return this;
    }

    public SQLiteDatabase getDatabase() {
        if (database[0] != null) {
            return database[0];
        } else if (database[1] != null) {
            return database[1];
        } else {
            openWritable();
            return database[1];
        }
    }

    public void close() {
        if (BuildConfig.DEBUG) Log.i(TAG, "--数据库-释放");
        ReentrantReadWriteLock.WriteLock w = writeLock;
        writeLock = null;
        MyDBLock.getInstance().unlock(w);
        //开启定时器
        closeRun();
    }

    private void closeRun() {
        closeTime = 3000;
        closeRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    if (BuildConfig.DEBUG) e.printStackTrace();
                }
                if (closeRunnable == this) {
                    if (writeLock != null) return;
                    closeTime -= 300;
                    if (closeTime > 0) {
                        TotpApp.runThread(this);
                    } else close_();
                }
            }
        };
        TotpApp.runThread(closeRunnable);
    }

    private void close_() {//3秒钟
        synchronized (DbConnect.class) {
            if (writeLock != null) return;
            if (database[0] != null) {
                database[0].close();
                database[0] = null;
            }
            if (database[1] != null) {
                database[1].close();
                database[1] = null;
            }
            if (dbHelp == null) return;
            dbHelp.close();
            dbHelp = null;
            if (BuildConfig.DEBUG) Log.v(TAG, "--数据库-关闭");
        }
    }
}
